﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Collections;

namespace Sys.Api
{
    /// <summary>
    /// Json处理
    /// </summary>
    public static class JsonHelper
    {
        /// <summary>
        /// 转换为Json格式
        /// </summary>
        /// <param name="obj">要转换的对象</param>
        /// <returns>结果Json对象</returns>
        public static string ToJson(object obj)
        {
            IsoDateTimeConverter timeConverter = new IsoDateTimeConverter();
            timeConverter.DateTimeFormat = "yyyy'-'MM'-'dd' 'HH':'mm':'ss";

            return JsonConvert.SerializeObject(obj, timeConverter);
        }

        /// <summary>
        /// 使Json反序列化
        /// </summary>
        /// <typeparam name="T">反序列化类型</typeparam>
        /// <param name="obj">Json</param>
        /// <returns>结果对象</returns>
        public static T Deserialize<T>(string obj)
        {
            return JsonConvert.DeserializeObject<T>(obj);
        }
    }
}
